﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Pomelo.EntityFrameworkCore.MySql.Metadata.Internal;
using TencentCloud.Ame.V20190916.Models;
using TencentCloud.Tdmq.V20200217.Models;
using ZenSystemService.Api.Business;
using ZenSystemService.Api.Common.Auth;
using ZenSystemService.Api.DTOs;
using ZenSystemService.Api.DTOs.Auth;
using ZenSystemService.Api.Services;

namespace ZenSystemService.Api.Controllers
{
    [Route("api/auth")]
    [ApiController]
    public class AuthController : ControllerBase
    {
        private readonly AuthService _authService;
        private readonly TokenService _tokenService;
        private readonly ProductService _productService;

        public AuthController(AuthService authService, TokenService tokenService, ProductService productService)
        {
            _authService = authService;
            _tokenService = tokenService;
            _productService = productService;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="login"></param>
        /// <returns></returns>
        [HttpPost("login")]
        public async Task<ActionResult> Login(LoginDTO login)
        {
            ReturnObject _returnObject = new ReturnObject();
            
            var user = await _authService.AuthenticateAsync(login);

            if (user == null)
            {
                Unauthorized();
            }
            else
            {
                _returnObject.Code = 1;
                //await _authService.SignInAsync(user, login.RememberMe);
                //var token = _tokenService.GenerateToken(user);
                return Ok(user);
            }

            return BadRequest();
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="login"></param>
        /// <returns></returns>
        [HttpPost("clientlogin")]
        public async Task<ActionResult> ClientLogin(ClientLoginRequestDTO login)
        {
            ReturnObject _returnObject = new ReturnObject();

            var loginResult = await _authService.AuthenticateClientAsync(login);

            if (loginResult == null)
            {
                Unauthorized();
            }
            else
            {
                _returnObject.Code = 1;
                //await _authService.SignInAsync(user, login.RememberMe);
                //var token = _tokenService.GenerateToken(user);
                return Ok(loginResult);
            }

            return BadRequest();
        }

        /// <summary>
        /// 用户登出
        /// </summary>
        /// <returns></returns>
        [HttpPost("logout")]
        public async Task<IActionResult> Logout()
        {
            //token验证

            //await _authService.SignOutAsync();
            //return Ok(new { message = "Logged out successfully." });
            try
            {
                //set token retaired
            }
            catch (Exception ex)
            {
                return StatusCode(500, "Internal server error.");
            }

            return Ok(new { Message = "Logout successful" });
        }

        /// <summary>
        /// 用户登出
        /// 解绑产品
        /// </summary>
        /// <returns></returns>
        [HttpPost("clientlogout")]
        public async Task<IActionResult> ClientLogout(ClientLogoutRequestDTO req)
        {
            await _productService.UnbindProductAsync(req);
            return Ok(new { Message = "Logout successful" });
        }

        [HttpGet("accessdenied")]
        public IActionResult AccessDenied()
        {
            return StatusCode(StatusCodes.Status403Forbidden, new { message = "Access Denied." });
        }


    }
}